﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;

#region "Custom namespace declarations"
using Comkarl;
#endregion

namespace Comkarl.Architecture.Patterns
{
    /// <summary>
    /// Represents a pattern member within an instance.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the pattern instance member identifier's name.
    /// </typeparam>
    /// <typeparam name="TPatternInstance">
    /// The type for the pattern instance.
    /// </typeparam>
    [Serializable]
    public abstract class PatternInstanceMember<TIdentifierName, TPatternInstance> : PatternMember<TIdentifierName> 
        where TIdentifierName : IEquatable<TIdentifierName> 
        where TPatternInstance : PatternInstance<TIdentifierName>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="id">
        /// The unique identifier for the newly-instantiated pattern instance member.
        /// </param>
        public PatternInstanceMember(PatternInstanceMemberIdentifier<TIdentifierName> id)
            : base(id)
        {
        }

        /// <summary>
        /// Retrieves the instance to which the current pattern instance member is associated with.
        /// </summary>
        private TPatternInstance associatedInstance;

        #region "Overridable methods"
        /// <summary>
        /// Associates the current pattern instance member to a provided pattern instance.
        /// </summary>
        /// <param name="someInstance"></param>
        public virtual void AssociateToInstance(TPatternInstance someInstance)
        {
            // Checks the reference and evaluates if it's null. If true, no association can be performed.
            if (someInstance == null)
            {
                throw new NullAssociatedPatternInstanceException<TIdentifierName, TPatternInstance>(
                    this,
                    new ArgumentNullException("someInstance")
                );
            }

            // The current pattern instance member can be associated to a pattern instance if this operation were not done before.
            if (AssociatedInstance == null)
            {
                this.associatedInstance = someInstance;
            }
            else
            {
                throw new PatternInstanceMemberAlreadyAssociatedToInstanceException<TIdentifierName, TPatternInstance>(
                    associatedInstance,
                    this,
                    new ArgumentException(PatternInstanceMemberAlreadyAssociatedToInstanceException<TIdentifierName, TPatternInstance>.GetLocalizedStringByID("InvalidPatternInstanceMemberInstanceAssociationArgument"), "someInstance")
                );

            }
        }
        #endregion

        /// <summary>
        /// Gets the instance to which the current pattern instance member is associated with
        /// </summary>
        public virtual TPatternInstance AssociatedInstance
        {
            get
            {
                return associatedInstance;
            }
        }
    }
}
